package com.dw.example.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.dw.example.R;

public class MainActivity extends Activity {

  public static final String CATEGORY = "dw.example.category";
  public static final String PATH = "dw.example.path";

  public static final String ICON = "dw.example.icon";
  public static final String TITLE = "dw.example.title";
  public static final String INTENT = "dw.example.intent";

  private ListView lv;
  private List<Map<String, Object>> data;
  private PackageManager pm;
  private SimpleAdapter adapter;
  private String path = "";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    pm = this.getPackageManager();

    lv = (ListView) findViewById(R.id.lv_activity_main);
    data = new ArrayList<Map<String,Object>>();
    adapter = new SimpleAdapter(this, data, R.layout.main_lv_item, new String[] { ICON, TITLE }, new int[] {
        R.id.icon, R.id.title });
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = (Intent) data.get(position).get(INTENT);
        if(null != intent){
          MainActivity.this.startActivity(intent);
        }else {
          path = data.get(position).get(PATH).toString();
          initData();
        }
      }
    });
    
    initData();
  }
  
  @Override
  public boolean onKeyUp(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK){
      if(path.length() > 1){
        path = path.substring(0, path.length()-1);
        path = path.substring(0, path.lastIndexOf("/"));
        initData();
        return true;
      }
    }
    
    return super.onKeyUp(keyCode, event);
  }

  private void initData() {
    if(null != path && !"".equals(path)){
      setTitle(path.substring(1));
    }
    
    path = path + "/";
    List<Map<String, Object>> list  = new ArrayList<Map<String,Object>>();
    
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(CATEGORY);
    
    List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
    Set<String> set = new HashSet<String>();
    
    for(int i = 0, len = infos.size(); i < len; i++){
      String label = infos.get(i).loadLabel(pm).toString();
      if(!label.startsWith(path)){
        label = "/" + label;
      }
      if(label.startsWith(path)){
        String str = label.substring(path.length());
        String[] arr = str.split("/");
        Map<String, Object> map = new HashMap<String, Object>();
        if(arr.length > 1 && !set.contains(arr[0])){
          map.put(ICON, R.drawable.folder);
          map.put(TITLE, arr[0]);
          map.put(PATH, path+arr[0]);
          list.add(map);
          set.add(arr[0]);
        }else if(arr.length == 1){
          map.put(ICON, R.drawable.file);
          map.put(TITLE, arr[0]);
          Intent intent2 = new Intent();
          intent2.setClassName(this, infos.get(i).activityInfo.name);
          map.put(INTENT, intent2);
          list.add(map);
        }
      }
    }
    data.clear();
    data.addAll(list);
    adapter.notifyDataSetChanged();
  }
}
